package com.qianyitian.hope2.analyzer.controller;

import com.alibaba.fastjson.JSON;
import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.service.StockService;
import com.qianyitian.hope2.analyzer.util.BarUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.DayOfWeek;
import java.util.*;

@RestController
public class DiversifiedChartController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StockService stockService;

    public DiversifiedChartController() {
    }


    @GetMapping("/week-range-chart")
    @CrossOrigin
    public String weekRangeChart(@RequestParam(value = "days", required = false, defaultValue = "250") Integer days, @RequestParam(value = "code") String code) {
        Stock stock = stockService.getStockDailySA(code);

        List<KLineInfo> kLineInfos = BarUtil.getRecentKLine(stock, days);
        stock.setkLineInfos(kLineInfos);

        Map<DayOfWeek, WeekChartItem> rangeMap = new LinkedHashMap<>();
        rangeMap.put(DayOfWeek.MONDAY, new WeekChartItem());
        rangeMap.put(DayOfWeek.TUESDAY, new WeekChartItem());
        rangeMap.put(DayOfWeek.WEDNESDAY, new WeekChartItem());
        rangeMap.put(DayOfWeek.THURSDAY, new WeekChartItem());
        rangeMap.put(DayOfWeek.FRIDAY, new WeekChartItem());


        for (KLineInfo kLineInfo : kLineInfos) {
            DayOfWeek dayOfWeek = kLineInfo.getDate().getDayOfWeek();
            WeekChartItem weekChartItem = rangeMap.get(dayOfWeek);
            update(weekChartItem, kLineInfo);
        }

        Map<String, Object> map = new HashMap<>(3);
        map.put("name", stock.getName());
        map.put("data", rangeMap);
        map.put("code", code);
        return JSON.toJSONString(map);
    }

    private void update(WeekChartItem weekChartItem, KLineInfo kLineInfo) {
        weekChartItem.setTotalNumber(weekChartItem.getTotalNumber() + 1);
        weekChartItem.setTotolRange(weekChartItem.getTotolRange() + kLineInfo.getChangePercent());
        if (kLineInfo.getChangePercent() > 0) {
            weekChartItem.setUpNumber(weekChartItem.getUpNumber() + 1);
        } else if (kLineInfo.getChangePercent() < 0) {
            weekChartItem.setDownNumber(weekChartItem.getDownNumber() + 1);
        }
    }
}

class WeekChartItem {
    int totalNumber;
    int upNumber;
    int downNumber;
    double totolRange;
    double avgRange;

    public double getAvgRange() {
        return totolRange / totalNumber;
    }

    public int getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(int totalNumber) {
        this.totalNumber = totalNumber;
    }

    public int getUpNumber() {
        return upNumber;
    }

    public void setUpNumber(int upNumber) {
        this.upNumber = upNumber;
    }

    public int getDownNumber() {
        return downNumber;
    }

    public void setDownNumber(int downNumber) {
        this.downNumber = downNumber;
    }

    public double getTotolRange() {
        return totolRange;
    }

    public void setTotolRange(double totolRange) {
        this.totolRange = totolRange;
    }
}


